package org.come.Jpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.math.BigDecimal;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.come.bean.OneArenaNotes;
import org.come.until.ScrollUI;
import org.come.until.SrcollPanelUI;

import com.tool.ModerateTask.TaskRoleData;
import com.tool.btn.FormsOnOffBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

/**
 * @author HGC<br>
 * @time 2020年3月20日 上午9:44:24<br>
 * @class 类名:PartnerArenaWarPanel 伙伴竞技战报面板 编号: 80<br>
 */
public class PartnerArenaWarPanel extends JPanel {

    private RichLabel rabRecord;
    private JScrollPane scrollPane;
    private JList<PartnerArenaModelPanel> partnerUnitJpanels;
    private DefaultListModel<PartnerArenaModelPanel> model;

    public PartnerArenaWarPanel() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            setPreferredSize(new Dimension(427, 375));
            setOpaque(false);
            setLayout(null);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/button/1.png", 1, 80);
            offBtn.setBounds(427 - 37, 10, 25, 25);
            this.add(offBtn);

            getScrollPane();
            getRabRecord();
        }else {
            setPreferredSize(new Dimension(427, 375));
            setOpaque(false);
            setLayout(null);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/s74.png", 1, 80);
            offBtn.setBounds(402, 5, 25, 25);
            this.add(offBtn);

            getScrollPane();
            getRabRecord();
        }
    }

    /** 展示战报详情 */
    public void showView(List<OneArenaNotes> arenaNotes) {
        if (arenaNotes == null)
            return;
        int componentCount = model.getSize();
        int size = arenaNotes.size();
        BigDecimal role_id = RoleData.getRoleData().getLoginResult().getRole_id();
        for (int i = 0; i < size; i++) {
            if (componentCount < size) {
                PartnerArenaModelPanel modelPanel = new PartnerArenaModelPanel(1);
                if (componentCount == 0)
                    model.addElement(modelPanel);
                else
                    model.add(componentCount, modelPanel);
                componentCount++;
            } else if (componentCount > size) {
                model.remove(componentCount);
                componentCount--;
            }
            PartnerArenaModelPanel modelPanel =  model.get(i);
            modelPanel.showWarView(arenaNotes.get(i), role_id);
        }
        partnerUnitJpanels.setPreferredSize(new Dimension(343, 43 * size));
        refreshRecord();
    }

    /** 添加实时战报 */
    public void refreshOneNoets(OneArenaNotes notes) {
        int componentCount = model.getSize();
        PartnerArenaModelPanel modelPanel = new PartnerArenaModelPanel(1);
        modelPanel.showWarView(notes, RoleData.getRoleData().getLoginResult().getRole_id());
        if(componentCount == 0){
            model.addElement(modelPanel);
        }else{
            model.add(0,modelPanel);
        }
    }


    /** 刷新胜场以及负场 */
    public void refreshRecord() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("#W当前胜利#R");
        int sumReceive = TaskRoleData.SumReceive(4, 2);
        buffer.append(sumReceive);
        buffer.append("#W场，负#R");
        buffer.append(TaskRoleData.SumReceive(4, 1)-sumReceive);
        buffer.append("#W场");
        rabRecord.setText(buffer.toString());
    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (icon == null) {
                icon = new ImageIcon("inkImg/background/S180.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 427, 375, null);
        }else {
            if (icon == null) {
                icon = new ImageIcon("img/xy2uiimg/S180.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 427, 375, null);
        }
    }

    public RichLabel getRabRecord() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (rabRecord == null) {
                rabRecord = new RichLabel("#K当前胜利#R17#K场，负#R6#K场", UIUtils.TEXT_FONT1);
                rabRecord.setBounds(50, 16, 300, 20);
                this.add(rabRecord);
            }
        }else {
            if (rabRecord == null) {
                rabRecord = new RichLabel("#W当前胜利#R17#W场，负#R6#W场", UIUtils.TEXT_FONT1);
                rabRecord.setBounds(50, 25, 300, 20);
                this.add(rabRecord);
            }
        }
        return rabRecord;
    }

    public void setRabRecord(RichLabel rabRecord) {
        this.rabRecord = rabRecord;
    }

    public JScrollPane getScrollPane() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (scrollPane == null) {
                // 属性滚动条
                scrollPane = new JScrollPane();
                scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                scrollPane.getViewport().setView(getPartnerUnitJpanels());
                scrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
                scrollPane.getVerticalScrollBar().setUnitIncrement(20);
                scrollPane.getViewport().setOpaque(false);
                scrollPane.setOpaque(false);
                scrollPane.setBounds(47, 44, 356 + 3, 306);
                scrollPane.setBorder(BorderFactory.createEmptyBorder());
                scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                this.add(scrollPane);
            }
        }else {
            if (scrollPane == null) {
                // 属性滚动条
                scrollPane = new JScrollPane();
                scrollPane.setVerticalScrollBarPolicy(22);
                scrollPane.getViewport().setView(getPartnerUnitJpanels());
                scrollPane.getVerticalScrollBar().setUI(new ScrollUI());
                scrollPane.getVerticalScrollBar().setUnitIncrement(20);
                scrollPane.getViewport().setOpaque(false);
                scrollPane.setOpaque(false);
                scrollPane.setBounds(34, 60, 356 + 10, 287);
                scrollPane.setBorder(BorderFactory.createEmptyBorder());
                scrollPane.setHorizontalScrollBarPolicy(31);
                this.add(scrollPane);
            }
        }
        return scrollPane;
    }

    public void setScrollPane(JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
    }

    public JList<PartnerArenaModelPanel> getPartnerUnitJpanels() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (partnerUnitJpanels == null) {
                partnerUnitJpanels = new JList<PartnerArenaModelPanel>();
                partnerUnitJpanels.setSelectionBackground(new Color(122, 117, 112));
                partnerUnitJpanels.setSelectionForeground(Color.white);
                // partnerUnitJpanels.setForeground(Color.white);
                // partnerUnitJpanels.setFont(UIUtils.TEXT_HY16);
                partnerUnitJpanels.removeAll();
                partnerUnitJpanels.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
                DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer() {
                    @Override
                    public Component getListCellRendererComponent(javax.swing.JList<?> list, Object value, int index,
                                                                  boolean isSelected, boolean cellHasFocus) {
                        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                        if (value instanceof PartnerArenaModelPanel) {
                            PartnerArenaModelPanel unitJpanel = (PartnerArenaModelPanel) value;
                            return unitJpanel;
                        }
                        return this;
                    }
                };
                partnerUnitJpanels.setCellRenderer(cellRenderer);
                partnerUnitJpanels.setOpaque(false);
                model = new DefaultListModel<>();
                partnerUnitJpanels.setModel(getModel());
            }
        }else {
            if (partnerUnitJpanels == null) {
                partnerUnitJpanels = new JList<PartnerArenaModelPanel>();
                partnerUnitJpanels.setSelectionBackground(new Color(122, 117, 112));
                partnerUnitJpanels.setSelectionForeground(Color.white);
                // partnerUnitJpanels.setForeground(Color.white);
                // partnerUnitJpanels.setFont(UIUtils.TEXT_HY16);
                partnerUnitJpanels.removeAll();
                partnerUnitJpanels.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
                DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer() {
                    @Override
                    public Component getListCellRendererComponent(javax.swing.JList<?> list, Object value, int index,
                                                                  boolean isSelected, boolean cellHasFocus) {
                        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                        if (value instanceof PartnerArenaModelPanel) {
                            PartnerArenaModelPanel unitJpanel = (PartnerArenaModelPanel) value;
                            return unitJpanel;
                        }
                        return this;
                    }
                };
                partnerUnitJpanels.setCellRenderer(cellRenderer);
                partnerUnitJpanels.setOpaque(false);
                model = new DefaultListModel<>();
                partnerUnitJpanels.setModel(getModel());
            }
        }
        return partnerUnitJpanels;
    }

    public void setPartnerUnitJpanels(JList<PartnerArenaModelPanel> partnerUnitJpanels) {
        this.partnerUnitJpanels = partnerUnitJpanels;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public DefaultListModel<PartnerArenaModelPanel> getModel() {
        if (model == null) {
            model = new DefaultListModel<>();
        }
        return model;
    }

    public void setModel(DefaultListModel<PartnerArenaModelPanel> model) {
        this.model = model;
    }

}
